'use strict';


import Base from './base.js';

const querystring = require('querystring');
var ttutil = require("../../business/ttutil");
var http = require('http');
const getToken = require('../../business/getwebtoken.js');
const getUserInfo = require('../../business/getwebuserinfo.js');

var SqlPool = require('../../business/sql_pool.js');
export default class extends Base {

    
    async indexAction() {
		var that=this;
        console.log("--------------"+this.param("code"));
        var openid="";
        var unionid="";
        var nickname="";
        try
        {
        //微信登录回调
        getToken(this.param("code")).then(function (data) {
			
            return JSON.parse(data);
            
            }).then(function (data) {
            getUserInfo(data['access_token'], data['openid']).then(  function (userinfo){
				//console.log("================"+JSON.stringify(userinfo));
                userinfo= JSON.parse(userinfo);
                openid=userinfo['openid'];
                unionid=userinfo['unionid'];
                nickname=userinfo['nickname'];
				
				
				
				
        var sql="select nickName,gameID,(select sum(payRMB) from ps_pay as p where p.userID=u.userID) as money,(select sum(payRMB) from ps_pay as p where p.userID=u.userID)*0.02 as present from ps_account as u where ";
        sql+=" recommendUserID in (select userID from ps_account where wxUnionID='"+unionid+"') order by money desc limit 0,100 ";
        SqlPool.x_query(sql,(err, rows, fields) => {
            var dbData = rows;
            var rowInfo = [];
            var keyArray = [];
//console.log("=======bbbbb========="+dbData.length);
            if (dbData!=null&&dbData.length > 0) {
                
                for (var key in  dbData[0]) {
                    keyArray.push(key);
                }
                rowInfo.push({link: null, key: keyArray[rowInfo.length], name: "玩家昵称"});
                rowInfo.push({link: null, key: keyArray[rowInfo.length], name: "游戏ID"});
                rowInfo.push({link: null, key: keyArray[rowInfo.length], name: "充值总额"});
                rowInfo.push({link: null, key: keyArray[rowInfo.length], name: "获取利润"});
            }
            that.assign("data", dbData || []);
            that.assign("rowInfo", rowInfo || []);


            return that.display("");

        });
				
				
                });
            });
			
			//console.log("=======aaaaaaaaaaaaaa========="+openid);
		
        

            }
            catch(err){}

		
        //that.display("");
    }
}